package com.item.yxlottery.yxlottery.fragment;

/*
 *------------------------------
 * @创建者      Administrator
 * @创建时间    2016/11/16 15:21
 * @文件描述    购彩fragment
 *------------------------------
 */

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.item.yxlottery.yxlottery.R;
import com.item.yxlottery.yxlottery.activity.ShuangSeQiuActivity;
import com.item.yxlottery.yxlottery.adapter.LotteryAdapter;
import com.item.yxlottery.yxlottery.base.BaseFragment;
import com.item.yxlottery.yxlottery.callback.MyStringCallBack;
import com.item.yxlottery.yxlottery.common.Constant;
import com.item.yxlottery.yxlottery.utils.UrlUtil;
import com.item.yxlottery.yxlottery.views.DividerItemDecoration;
import com.zhy.autolayout.utils.AutoUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

import static com.item.yxlottery.yxlottery.views.DividerItemDecoration.VERTICAL_LIST;

public class BuyLotteryFragment extends BaseFragment {

    @Bind(R.id.cb_buy_page)
    ConvenientBanner mCbPage;

    @Bind(R.id.rv_buy_list)
    RecyclerView mRvList;

    private ArrayList<String> mImageUrlList = new ArrayList<>();//存储图片url地址
    private ArrayList<String> mAdvUrlList = new ArrayList<>();//存储点击图片之后的广告的url地址
    private ArrayList<JSONObject> mLotteresList = new ArrayList<>();//存储获取到的所有彩种信息
    private LotteryAdapter mAdapter;

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_buy, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        setRecycelViewData();
        httpImageUrl(mCbPage);
        getLotteresInfo();
    }

    @Override
    protected void initEvent() {
        mCbPage.setOnItemClickListener(new com.bigkoo.convenientbanner.listener.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(mContext, mAdvUrlList.get(position), Toast.LENGTH_SHORT).show();
            }
        });

        mRvList.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdaptr, View view, int i) {
                String model = mLotteresList.get(i).getString("model");
                Toast.makeText(mContext, mLotteresList.get(i).getString("goodsName"), Toast.LENGTH_SHORT).show();
                if (model.equals(Constant.LOTTERY_TYPE_SSQ)) {//双色球
                    startActivity(new Intent(mContext, ShuangSeQiuActivity.class));
                } else if (model.equals(Constant.LOTTERY_TYPE_FC3D)) {//福彩3D

                } else if (model.equals(Constant.LOTTERY_TYPE_QLC)) {//七乐彩

                } else if (model.equals(Constant.LOTTERY_TYPE_DF6J1)) {//东方6+1

                } else if (model.equals(Constant.LOTTERY_TYPE_FJTC36X7)) {//福建体彩36选7

                } else if (model.equals(Constant.LOTTERY_TYPE_GDFC36X7)) {//广东福彩36选7

                } else if (model.equals(Constant.LOTTERY_TYPE_GDSZFC)) {//深圳风采

                } else if (model.equals(Constant.LOTTERY_TYPE_YZFCPL7)) {//燕赵风采排列7

                } else if (model.equals(Constant.LOTTERY_TYPE_XJFC35X7)) {//新疆风采35选7

                } else if (model.equals(Constant.LOTTERY_TYPE_CQSSC)) {//重庆时时彩

                } else if (model.equals(Constant.LOTTERY_TYPE_CQKLSF)) {//重庆幸运农场

                } else if (model.equals(Constant.LOTTERY_TYPE_SH11X5)) {//上海11选5

                } else if (model.equals(Constant.LOTTERY_TYPE_BJKL8)) {//北京快乐8

                } else if (model.equals(Constant.LOTTERY_TYPE_BJPK10)) {//北京Pk拾

                } else if (model.equals(Constant.LOTTERY_TYPE_GDKLSF)) {//广东快乐十分

                } else if (model.equals(Constant.LOTTERY_TYPE_GD11X5)) {//广东11选5

                } else if (model.equals(Constant.LOTTERY_TYPE_GXK3)) {//广西快3

                } else if (model.equals(Constant.LOTTERY_TYPE_GXKLSF)) {//广西快乐十分

                } else if (model.equals(Constant.LOTTERY_TYPE_JX11X5)) {//江西11选5

                } else if (model.equals(Constant.LOTTERY_TYPE_JSK3)) {//江苏老快3

                } else if (model.equals(Constant.LOTTERY_TYPE_HUNKLSF)) {//湖南快乐十分

                } else if (model.equals(Constant.LOTTERY_TYPE_DLT)) {//大乐透

                } else if (model.equals(Constant.LOTTERY_TYPE_QXC)) {//七星彩

                } else if (model.equals(Constant.LOTTERY_TYPE_PL3)) {//排列三

                } else if (model.equals(Constant.LOTTERY_TYPE_PL5)) {//排列五

                } else if (model.equals(Constant.LOTTERY_TYPE_BJDC)) {//北京单场

                } else if (model.equals(Constant.LOTTERY_TYPE_SFGG)) {//胜负过关

                } else if (model.equals(Constant.LOTTERY_TYPE_JCZQ)) {//竞彩足球

                } else if (model.equals(Constant.LOTTERY_TYPE_JCLQ)) {//竞彩篮球

                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        mCbPage.startTurning(3500);//3.5S自动翻页
    }

    @Override
    public void onPause() {
        super.onPause();
        mCbPage.stopTurning();//停止自动翻页
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    /*- 获取轮播图图片url -*/
    private void httpImageUrl(final ConvenientBanner cbPage) {
        OkHttpUtils.post().url(UrlUtil.getLotteryAdvListUrl()).addParams("pageNo", "0").addParams("pageSize", "10").build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        JSONObject parse = (JSONObject) JSON.parse(response);
                        if (parse.getString("error_code").equals(Constant.SUCCESS)) {//返回的成功数据
                            for (Object object : JSON.parseArray(parse.getString("datas"))) {
                                JSONObject jsonObject = (JSONObject) object;
                                mImageUrlList.add(UrlUtil.getImageHomeUrl() + jsonObject.getString("resUrl"));
                                mAdvUrlList.add(jsonObject.getString("advUrl"));
                            }
                        }
                    }

                    @Override
                    public void onAfter(int id) {
                        super.onAfter(id);
                        setConvenientBanner(cbPage);
                    }
                });
    }

    /*- 设置左右轮播图内容 -*/
    private void setConvenientBanner(ConvenientBanner cbPage) {
        cbPage.setPages(new CBViewHolderCreator() {
            @Override
            public Object createHolder() {
                return new NetWorkImageHolderView();
            }
        }, mImageUrlList)
                //设置两个点作为翻页指示器
                .setPageIndicator(new int[]{R.drawable.shap_fragment_buy_pageindicator, R.drawable.shap_fragment_buy_pageindicator_selector})
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT);//设置指示器的方向
        cbPage.setManualPageable(true);//设置可以手动影响
        AutoUtils.autoSize(cbPage);
    }

    /*- ConvenientBanner网络加载图片 -*/
    public class NetWorkImageHolderView implements Holder<String> {
        private ImageView mImageView;

        @Override
        public View createView(Context context) {
            mImageView = new ImageView(context);
            mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return mImageView;
        }

        @Override
        public void UpdateUI(Context context, int position, String data) {
            Glide.with(context).load(data).into(mImageView);
        }
    }

    /*- 获取彩种信息 -*/
    private void getLotteresInfo() {
        OkHttpUtils.post().url(UrlUtil.getLotteryTypesUrl()).addParams("pageNo", "1").addParams("pageSize", "30").build()
                .execute(new MyStringCallBack(mContext, true) {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        JSONObject parse = (JSONObject) JSON.parse(response);
                        if (parse.getString("error_code").equals(Constant.SUCCESS)) {
                            for (Object object : JSON.parseArray(parse.getString("datas"))) {
                                mLotteresList.add((JSONObject) object);
                            }
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                });
    }

    private void setRecycelViewData() {
        mAdapter = new LotteryAdapter(mContext, R.layout.item_fragment_buy_list, mLotteresList);
        mAdapter.openLoadAnimation();//开启默认渐显动画
        mRvList.setLayoutManager(new GridLayoutManager(mContext, 4));
        mRvList.addItemDecoration(new DividerItemDecoration(mContext, VERTICAL_LIST, 0xffC3C3C3, 1));//分割线
        mRvList.setAdapter(mAdapter);
    }
}